package algotithm.leetcode.bit.test260;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/24-9:49
 * @URL https://leetcode-cn.com/problems/single-number-iii/
 * @Title 260. 只出现一次的数字 III
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,2,1,3,2,5};
        int[] arr = solution.singleNumber(nums);
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    public int[] singleNumber(int[] nums) {
        int one = 0, two = 0;
        int xor = 0;
        for (int num : nums) {
            xor ^= num;
        }
        int mask = xor & (-xor);
        for (int num : nums) {
            if ((num & mask) == 0) {
                one ^= num;
            } else {
                two ^= num;
            }
        }
        return new int[]{one,two};
    }

}
